package jtalk.model;

import java.util.Collection;

import jtalk.controller.Properties;
import jtalk.logging.Logger;

import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.packet.Presence;

public class RosterStatusListener implements RosterListener {

	private XMPPManager xmppManager = null;	
	
	protected RosterStatusListener(XMPPManager xmppManager) {
		this.xmppManager = xmppManager;		
	}
	
	public void entriesAdded(Collection<String> addresses) {
		//TODO
		for(String addresse : addresses) {
			System.out.println("Roster listener -> " +addresse);
		}		
	}

	public void entriesDeleted(Collection<String> addresses) {
		//TODO
		for(String addresse : addresses) {
			System.out.println("Roster listener -> " +addresse);
		}
		
	}

	public void entriesUpdated(Collection<String> addresses) {
		//TODO
		for(String addresse : addresses) {
			System.out.println("Roster listener -> " +addresse);
		}		
	}

	public void presenceChanged(Presence presence) {
		
	
	
		Contact contact = new Contact(presence.getFrom());
		
		//Set contact avaibility
		contact.setAvailable(presence.isAvailable());
		
		//Set other status
		if(presence.isAway()) {
			contact.setStatus(Properties.STATUS_AWAY.value());		
		}
		//TODO
		//else if(presence.getMode().equals(Presence.Mode.dnd)) {
		//		contact.setStatus(Properties.STATUS_BUSY.value());
		//}
		
		xmppManager.firePropertyChange(Properties.CONTACT_PRESENCE.toString(), null, contact);
		Logger.getLogger(this).debug("Presence message : " + presence.getFrom() + " is " + presence);
		
		
						
	}

}
